package com.gitee.feizns.web.mvc.validate;

import com.baomidou.mybatisplus.mapper.Condition;
import com.gitee.feizns.web.dao.ar.AbstractModel;
import com.gitee.feizns.web.mvc.validate.annotation.Unique;

/**
 * @author feizns
 * @since 2019/8/12
 */
public class UniqueValidator extends AbstractConstraintValidator<Unique, Object> {

    @Override
    public boolean validate(AbstractModel model, String field, Object value, String where) {
        return model.exists(Condition.create().where(where).eq(field, value)) == false;
    }

    @Override
    public String getMessage(Class<? extends AbstractModel> type, String field, Object value) {
        return String.format("%s %s '%s' already exists.", type.getSimpleName(), field, value);
    }

}
